$(document).ready(function(e) {
    leftNav();
	selectF();
	chooseAllF();
	treeF();
	addAndDeleteF();
	 inputSsF();
	 
	 addJG();//添加机构按钮
});
function leftNav(){
	if($("#left_nav").length<=0){ return null;}
	var leftLi = $("#left_nav>li");
	leftLi.eq(0).addClass("current");
	// leftLi.children(".left_sub").children("a").eq(0).addClass("on");
	leftLi.children(".left_a").click(function(){
		var lis = $(this).parent();
		lis.prevAll("li").removeClass("current").children(".left_sub").slideUp();
		lis.nextAll("li").removeClass("current").children(".left_sub").slideUp();
		lis.addClass("current");
		lis.children(".left_sub").slideDown();	
		leftLi.children(".left_sub").children("a").removeClass("on");
		lis.children(".left_sub").children("a").eq(0).addClass("on");
	});
	leftLi.children(".left_sub").children("a").click(function(){
		leftLi.children(".left_sub").children("a").removeClass("on"); 
		$(this).addClass("on")
	});
}

function selectF(){
	if($(".selects").length<=0){ return null;}
	var selects = $(".selects");
	selects.children("dt").click(function(event){  
		$(this).next("dd").show();	
		$(this).parent().css({"zIndex":20});
		 event.stopPropagation();
	});
	selects.hover(function(){},function(event){
		 $(this).children("dd").hide();	
		 $(this).css({"zIndex":1});
	});
	var a =selects.children("dd").find("a");
	a.click(function(event){
		var v = $(this).html(); 
		var dd =$(this).parent().parent();
		dd.parent().children("input").val(v);
		dd.parent().children("dt").children("span").html(v);
		dd.hide();
		event.stopPropagation();
	});
}

//全选函数
function chooseF(obj){ 
	var num = $(obj).length;
	for(var i = 0 ; i<num ; i++){
		$(obj)[i].checked = true ;
	}
}
//取消选择
function unchooseF(obj){  
	var num = $(obj).length;
	for(var i = 0 ; i<num ; i++){
		$(obj)[i].checked = false ;
	}
}
//反选
function abchooseF(obj){
	var num = $(obj).length; 
	for(var i = 0 ; i<num ; i++){
		if(!!$(obj)[i].checked){
			$(obj)[i].checked = false
		 }else{		 
			$(obj)[i].checked = true;
		 }
	}
}

function chooseAllF(){
 	if($("#chooseAll").length<=0){ return null;}
	$("#chooseAll").click(function(){
		  var t = $(this)[0];
		  //alert(t.checked);
		  if(t.checked){
				chooseF(".chooseOne");  
			}else{
				unchooseF(".chooseOne");  
			}
	});
}
function treeF(){
	if($(".trees").length<=0){ return null;}
	$(".trees .tree_box").click(function(){
		var t = $(this)[0];
		var subboxes = $(this).parent().next("dd").find(".tree_box");
		var  xd = $(this).parent().parent().parent();
		var inputs= xd.children("dl").children("dt").children("input");
		var num = subboxes.length;
		var i = 0; 
		var k = 0;  
		var kong =0;
		 if(t.checked){
				  for( i=0; i<num ; i++){
						subboxes[i].checked = true ;
					}
					//
					
					//
			}else{
				  for( i=0; i<num ; i++){
						subboxes[i].checked = false ;
					}
			}
		for(k=0; k<inputs.length ; k++){
						if(inputs[k].checked){
							kong++; 
						}
					}
		if(kong){
			xd.prev("dt").children("input")[0].checked = true;	 
		}else{
			xd.prev("dt").children("input")[0].checked = false;  
		}
	});
	$(".tree_open").click(function(){
		$(this).toggleClass("tree_o");
		$(this).parent().next("dd").toggle();	
	});
}
//添加和删除按钮
function addAndDeleteF(){
	if($(".addAndDelete").length<=0){ return null;}
	var a_d = $(".addAndDelete");
	var addBtn = a_d.children(".JS_addBtn");
	var delBtn = a_d.children(".JS_deleteBtn");
	addBtn.click(function(){
		 var t = $(this);
		 var clone ="#"+t.attr("clone");
		 var tag = "#"+t.attr("tag");
		
		 var md = $( $(clone).html() );
		 var spans = md.find(".JS_duotu_btn"); //找到flash所在span
		 spans.attr("id","JS_upbtn");
		 var imgs = md.find(".JS_duotuimg"); 
		 imgs.attr("nowpic",0);
		 md.clone(true).appendTo(tag);
		 //createUpBtn(回调函数，图片)
		 // createUpBtn("doFileDuotu",".JS_duotuimg","JS_upbtn");//创建flash btn
		// createUpBtn("doFileDuotu",".JS_duotuimg",".JS_upbtn");
		 
		  var tm =$(tag).children(".templete");
		  var temp_num = tm.length; 
		tm.eq(temp_num-1).find(".JS_stepNum").html(temp_num);
		tm.eq(temp_num-1).find(".JS_setSort").val(temp_num);
	});
	delBtn.click(function(){
		var t = $(this);
		var tag = "#"+t.attr("tag");
		var tm =$(tag).children(".templete");
		var temp_num = tm.length; 
		if(temp_num<=1){
			 return null;
			}
		tm.eq(temp_num-1).remove();
	});
}

function inputSsF(){
	if($(".JS_s_Input").length<=0){ return null;}
	 var stm = null
	$(".JS_s_Input").keyup(function(){
		var t = $(this);
		var nr =  t.parent().children(".JS_s_nr");
		stm=setTimeout(function(){
			nr.show();
		},500); 
		var i_nr = $.trim(t.val());
		i_nr = i_nr.replace(/\s/ig,"")
		if(i_nr==""){
				nr.hide(); clearTimeout(stm);return null;
		} 
		var nr_reg =new RegExp(i_nr,"ig"); 
		var span_reg = /<span class=\'c_red\'>|<span class=\"c_red\">|<\/span>|\//ig
		var lis = nr.children("li");
		var nr_num = lis.length;
		for(var i = 0 ; i<nr_num ; i++){
			lis[i].innerHTML=lis[i].innerHTML.replace(span_reg,"");	 
			lis[i].innerHTML=lis[i].innerHTML.replace(nr_reg,"<span class='c_red'>"+i_nr+"</span>");	
		}
	});	
	$(".JS_s_Input").blur(function(){
		var t = $(this);
		var nr =  t.parent().children(".JS_s_nr");
		nr.hide();
	}); 
}

function   addJG(){
	if($("#JS_addjg_btn").length<=0){ return null;}
	var  jg_table=$("#team_Ups>table");
	var jg_num = jg_table.eq(jg_table.length-1).find(".JS_duotuimg").attr("id");
	 jg_num=Number(String(jg_num).substr(10))+1; 
	 
	var a_d = $("#JS_addjg_btn"); 
	a_d.click(function(){
		 jg_num++;
		 var t = $(this);	 
		 var clone ="#"+t.attr("clone");
		 var tag = "#"+t.attr("tag");
	
		 var md = $( $(clone).html() );
		 var spans = md.find(".JS_upbtn"); //找到flash所在span
		 spans.attr("id","JS_upbtn"+jg_num);
		 
		 var imgs = md.find(".JS_duotuimg"); 
		  imgs.attr("id","JS_jg_imgs"+jg_num);
		 md.clone(true).appendTo(tag);
		 //Flash_btn_jg1  ,flash的span
		 //JS_jg_imgs1,  图片id
		 //
		 //createUpBtn(回调函数，图片)
		  //createUpBtn("doFileDuotu",".JS_duotuimg","JS_upbtn");//创建flash btn
		  createUpBtn("doJG_up","JS_jg_imgs"+jg_num,"JS_upbtn"+jg_num);
		 
	});
	 
}

function artAlertDialog(contentVal,iconType,event){
	art.dialog({
		content:contentVal,
		icon:iconType,
		ok:function(){
			if(event == null){
				this.close()
			}else{
				eval(event);
			}
		}
	});
}